package com.he.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.pojo.LogComment;
import com.he.pojo.LogCommentExample;
import com.he.pojo.Role;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.pojo.UserLog;
import com.he.pojo.UserLogExample;
import com.he.pojo.Workflow;
import com.he.pojo.WorkflowExample;
import com.he.pojo.WorkflowStepDetail;
import com.he.pojo.WorkflowStepDetailExample;
import com.he.pojo.UserExample.Criteria;
import com.he.service.department.DepartmentServiceI;
import com.he.service.logComment.LogCommentServiceI;
import com.he.service.role.RoleServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.userLog.UserLogServiceI;
import com.he.service.workflow.WorkflowServiceI;
import com.he.service.workflowStepDetail.WorkflowStepDetailServiceI;

@Controller
@RequestMapping("person")
public class PersonalController {
	@Autowired
	UserServiceI userServiceI;
	@Autowired
	RoleServiceI roleServicel;
	@Autowired
	DepartmentServiceI departServiceI;
	@Autowired
	UserLogServiceI userLogserviceI;
	@Autowired
    LogCommentServiceI logcommentserviceI;
	@RequestMapping("/show")
	public String personShow(HttpServletRequest request,String sign) {
		User user=new User();
		user.setWithPage(0);
		HttpSession session = request.getSession();
		String useraccount=(String) session.getAttribute("useraccount");
        UserExample uex=new UserExample();
        uex.createCriteria().andUserAccountEqualTo(useraccount);
		List<User> list=userServiceI.getUsers(uex, user) ;
		for (User user2 : list) {
			session.setAttribute("user2", user2);
		}
		if(sign.equals("1"))
		   return "redirect:allshow.do"; 
		else
			return "redirect:logshow.do";
	}
	@RequestMapping("/updateshow")
	public String  Updateshow(ModelMap map,HttpServletRequest request) {
		User user=new User();
		HttpSession session = request.getSession();
		String useraccount=(String) session.getAttribute("useraccount");//获取session
		UserExample uex=new UserExample();
       Criteria uc = uex.createCriteria();
	   uc.andUserAccountEqualTo(useraccount);
	 	List<User> list=userServiceI.getUsers(uex, user) ;
		Role role=new Role();
		role.setWithPage(0);
		Department department=new Department();	
		department.setWithPage(0);
		List<Role> rlist = roleServicel.getRoles(null, role);
		List<Department> dlist = departServiceI.getDepartments(null, department);
	    map.addAttribute("user",list);
		map.addAttribute("rlist", rlist);
		map.addAttribute("dlist", dlist);
		return "profile";
		
	}
	@RequestMapping("/update")
	public String personUpdate(User user,HttpSession session) {
		
		int r=userServiceI.update(user);
		session.setAttribute("user", user);
		if(r>0)
		   return "redirect:show.do?sign=0";
		else
		   return "redirect:show.do?sign=0";
		
	}
	@Autowired
	private WorkflowStepDetailServiceI wServiceI;
	@RequestMapping("/logshow")
	public String  alllogShow(HttpServletRequest request,ModelMap modelMap) {
     	int resign=0;
		User user=new User();
		user.setWithPage(0);
		HttpSession session = request.getSession();
		User user2=new User();
		user2=(User)session.getAttribute("user");
		int userid=user2.getId();
        UserLogExample ule=new UserLogExample();
        com.he.pojo.UserLogExample.Criteria lc = ule.createCriteria();
        lc.andUserIdEqualTo(userid);
		UserLog userLog=new UserLog();
		WorkflowStepDetailExample wExample=new WorkflowStepDetailExample();
		wExample.createCriteria().andUserIdEqualTo(user2.getId());
		List<WorkflowStepDetail> wlist=wServiceI.getWorkflowStepDetails(wExample, new WorkflowStepDetail());
		//查询日志
		List<UserLog> llist= userLogserviceI.getUserLogs(ule, userLog);
		int count=llist.size();
		int wcount=wlist.size();
		modelMap.addAttribute("logcount", count);
		modelMap.addAttribute("wcount", wcount);
		List<UserLog> partlist = new ArrayList<>();
	//详细评论的查询
		Map<Integer, List<LogComment>> map = new HashMap<>();
	//查询到的5个log
	    for (UserLog userLog2 : llist)
	    {
	    	if(resign<5){
	    		/*   System.out.println("*************3**********"+userLog2.getId());*/
	    		    partlist.add(userLog2);
	    		    LogCommentExample lce=new LogCommentExample();
	    			com.he.pojo.LogCommentExample.Criteria clce = lce.createCriteria();
	    			LogComment logcomment=new LogComment();
	    		    clce.andLogIdEqualTo(userLog2.getId());
	    		    List<LogComment> lclist=logcommentserviceI.getLogComments(lce, logcomment);
	    		    map.put(userLog2.getId(),lclist);
	    		   resign++;
	    	} 
	  
	      }
	//
	     request.setAttribute("map", map);
	     request.setAttribute("clist", partlist);
	 
		return  "profile";
	}
	
	@RequestMapping("/allshow")
	public String  partlogShow(HttpServletRequest request) {
     	int resign=0;
		User user=new User();
		user.setWithPage(0);
		HttpSession session = request.getSession();
		String useraccount=(String) session.getAttribute("useraccount");
		 UserExample uex=new UserExample();
	     Criteria uc = uex.createCriteria();
		uc.andUserAccountEqualTo(useraccount);
		List<User> list=userServiceI.getUsers(uex, user) ;
		User us=(User) session.getAttribute("user");
		Integer userid=us.getId();
		for (User user2 : list) {
			userid=user2.getId();
		}
		//
        UserLogExample ule=new UserLogExample();
        com.he.pojo.UserLogExample.Criteria lc = ule.createCriteria();
        lc.andUserIdEqualTo(userid);
		UserLog userLog=new UserLog();
		List<UserLog> llist= userLogserviceI.getUserLogs(ule, userLog);
	//详细评论的查询
	Map< Integer,List<LogComment>> map=new HashMap<>();
	//查询到的5个log
	    for (UserLog userLog2 : llist)
	    {
	    		    LogCommentExample lce=new LogCommentExample();
	    			com.he.pojo.LogCommentExample.Criteria clce = lce.createCriteria();
	    			LogComment logcomment=new LogComment();
	    		    clce.andLogIdEqualTo(userLog2.getId());
	    		    List<LogComment> lclist=logcommentserviceI.getLogComments(lce, logcomment);
	    		    map.put(userLog2.getId(),lclist);
	    		   
	    
	     }
	//
	     request.setAttribute("map", map);
	     request.setAttribute("clist", llist);
	 
		return  "profile";
	}
}
